//
//  LZCouponTableViewBaseView.m
//  laziz_Merchant
//
//  Created by xbnzd on 16/12/19.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZCouponTableViewBaseView.h"
#import "UIScrollView+LZContentOffset.h"
#import "LZHorizontalBaseSubTableView.h"
#import "LZNotSubmittedCouponTableView.h"
#import "LZWaitPublishCouponTableView.h"
#import "LZPublishedCouponTableView.h"
#import "LZFinishedCouponTableView.h"
#import "LZPlayVideoManager.h"
#import "Masonry.h"

@interface LZCouponTableViewBaseView()<UIScrollViewDelegate,LZNotSubmitTableViewDelegate,LZWaitPublishTableViewDelegate,LZFinishTableViewDelegate,LZPublishTableViewDelegate>
@property (nonatomic, strong) LZNotSubmittedCouponTableView *notSubmitTable;
@property (nonatomic, strong) LZWaitPublishCouponTableView *waitPublishTable;
@property (nonatomic, strong) LZPublishedCouponTableView *publishedTable;
@property (nonatomic, strong) LZFinishedCouponTableView *finishedTable;
@property (nonatomic, assign) CouponListTitleStateCode currentCode;
@end

@implementation LZCouponTableViewBaseView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
        self.contentSize = CGSizeMake(SCREEN_WIDTH*4, SCREEN_HEIGHT);
        self.showsHorizontalScrollIndicator = NO;
        self.pagingEnabled = YES;
        self.scrollEnabled = NO;
        self.bounces = NO;
        
        [self configureUI];
    }
    
    return self;
}

#pragma mark -
#pragma mark public method
- (void)clickTitleBtnWithTag:(CouponListTitleStateCode)couponState withSellerId:(NSString *)sellerId
{
    if (self.currentCode != 0 && self.currentCode != couponState) {//
        LZHorizontalBaseSubTableView *lastTable = [self viewWithTag:self.currentCode];
        lastTable.isCurrentShowingTable = NO;
        [lastTable releasePlayViewRAM];
        [LZPlayVideoManager shareManager].playVideoManagerDelegate = nil;
    }
    LZHorizontalBaseSubTableView *table = [self viewWithTag:couponState];
    table.isCurrentShowingTable = YES;
    table.sellerId = sellerId;
    table.couponListCode = couponState;
    self.currentCode = couponState;
    [table checkHaveData];
    [self setContentOffset:CGPointMake(table.frame.origin.x, 0)];
}

- (void)tableViewNeedStopVideoWithCouponListCode:(CouponListTitleStateCode)couponListTitleCode
{
    LZHorizontalBaseSubTableView *table = [self viewWithTag:couponListTitleCode];
    [table releasePlayViewRAM];
}


#pragma mark -
#pragma mark 各个优惠列表点击跳转事件协议方法--返回到最上层vc跳转,回调回最上层vc操作的结果
- (void)notSubmitCellClickEditBtnOrDetailBtnWithModel:(LZCouponListModel *)dataModel couponEventCode:(CouponListEventCode)eventCode operationSuccess:(void (^)(CouponListEventCode, LZCouponListModel *))successBlock
{
    if (self.couponTableBaseViewDelegate && [self.couponTableBaseViewDelegate respondsToSelector:@selector(tableViewCellClickBtnWithModel:couponEventCode: couponListStateCode:operationSuccess:)]) {
        [self.couponTableBaseViewDelegate tableViewCellClickBtnWithModel:dataModel couponEventCode:eventCode couponListStateCode:kCoupon_notSubmit operationSuccess:successBlock];
    }
}

- (void)waitPublishCellClickEditBtnOrDetailBtnWithModel:(LZCouponListModel *)dataModel couponEventCode:(CouponListEventCode)eventCode operationSuccess:(void (^)(CouponListEventCode, LZCouponListModel *))successBlock
{
    if (self.couponTableBaseViewDelegate && [self.couponTableBaseViewDelegate respondsToSelector:@selector(tableViewCellClickBtnWithModel:couponEventCode: couponListStateCode:operationSuccess:)]) {
        [self.couponTableBaseViewDelegate tableViewCellClickBtnWithModel:dataModel couponEventCode:eventCode couponListStateCode:kCoupon_waitPublish operationSuccess:successBlock];
    }
}

- (void)finishCellClickCopyBtnOrDetailBtnWithModel:(LZCouponListModel *)dataModel couponEventCode:(CouponListEventCode)eventCode operationSuccess:(void (^)(CouponListEventCode, LZCouponListModel *))successBlock
{
    if (self.couponTableBaseViewDelegate && [self.couponTableBaseViewDelegate respondsToSelector:@selector(tableViewCellClickBtnWithModel:couponEventCode: couponListStateCode:operationSuccess:)]) {
        [self.couponTableBaseViewDelegate tableViewCellClickBtnWithModel:dataModel couponEventCode:eventCode couponListStateCode:kCoupon_hasBeenFinished operationSuccess:successBlock];
    }
}

- (void)publishCellClickDetailBtnWithModel:(LZCouponListModel *)dataModel couponEventCode:(CouponListEventCode)eventCode operationSuccess:(void (^)(CouponListEventCode, LZCouponListModel *))successBlock
{
    if (self.couponTableBaseViewDelegate && [self.couponTableBaseViewDelegate respondsToSelector:@selector(tableViewCellClickBtnWithModel:couponEventCode: couponListStateCode:operationSuccess:)]) {
        [self.couponTableBaseViewDelegate tableViewCellClickBtnWithModel:dataModel couponEventCode:eventCode couponListStateCode:kCoupon_hasBeenPublished operationSuccess:successBlock];
    }
}


#pragma mark -
#pragma mark 子tableview 操作提交,发布,下线,删除成功后,通知最上层vc 更新数量 数字
- (void)notSubmitCellClickBtnSuccessToRefeshSuperVCTitleCount
{
    if (self.couponTableBaseViewDelegate && [self.couponTableBaseViewDelegate respondsToSelector:@selector(tableViewCellClickBtnSuccessToRefreshSupeViewTitleCount)]) {
        [self.couponTableBaseViewDelegate tableViewCellClickBtnSuccessToRefreshSupeViewTitleCount];
    }
}

- (void)waitPublishCellClickBtnSuccessToRefeshSuperVCTitleCount
{
    if (self.couponTableBaseViewDelegate && [self.couponTableBaseViewDelegate respondsToSelector:@selector(tableViewCellClickBtnSuccessToRefreshSupeViewTitleCount)]) {
        [self.couponTableBaseViewDelegate tableViewCellClickBtnSuccessToRefreshSupeViewTitleCount];
    }
}

- (void)publishCellClickBtnSuccessToRefeshSuperVCTitleCount
{
    if (self.couponTableBaseViewDelegate && [self.couponTableBaseViewDelegate respondsToSelector:@selector(tableViewCellClickBtnSuccessToRefreshSupeViewTitleCount)]) {
        [self.couponTableBaseViewDelegate tableViewCellClickBtnSuccessToRefreshSupeViewTitleCount];
    }
}

- (void)finishCellClickBtnSuccessToRefeshSuperVCTitleCount
{
    if (self.couponTableBaseViewDelegate && [self.couponTableBaseViewDelegate respondsToSelector:@selector(tableViewCellClickBtnSuccessToRefreshSupeViewTitleCount)]) {
        [self.couponTableBaseViewDelegate tableViewCellClickBtnSuccessToRefreshSupeViewTitleCount];
    }
}

#pragma mark -
#pragma mark configure UI
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    [self setLanguageContentOffset];
}

- (void)configureUI
{
    UIView *bgView = [[UIView alloc] init];
    [self addSubview:bgView];
    [bgView addSubview:self.notSubmitTable];
    [bgView addSubview:self.waitPublishTable];
    [bgView addSubview:self.publishedTable];
    [bgView addSubview:self.finishedTable];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
        make.height.equalTo(self);
    }];
    
    [self.notSubmitTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.equalTo(bgView);
        make.leading.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    [self.waitPublishTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.equalTo(bgView);
        make.leading.equalTo(self.notSubmitTable.mas_trailing);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    [self.publishedTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.equalTo(bgView);
        make.leading.equalTo(self.waitPublishTable.mas_trailing);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    [self.finishedTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.equalTo(bgView);
        make.leading.equalTo(self.publishedTable.mas_trailing);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.finishedTable);
    }];
}

#pragma mark -
#pragma mark 初始化
- (LZNotSubmittedCouponTableView *)notSubmitTable
{
    if (!_notSubmitTable) {
        _notSubmitTable = [[LZNotSubmittedCouponTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _notSubmitTable.tag = kCoupon_notSubmit;
        _notSubmitTable.notSubmitTableDelegate = self;
    }
    
    return _notSubmitTable;
}

- (LZWaitPublishCouponTableView *)waitPublishTable
{
    if (!_waitPublishTable) {
        _waitPublishTable = [[LZWaitPublishCouponTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _waitPublishTable.tag = kCoupon_waitPublish;
        _waitPublishTable.waitPublishTableDelegate = self;
    }
    
    return _waitPublishTable;
}

- (LZPublishedCouponTableView *)publishedTable
{
    if (!_publishedTable) {
        _publishedTable = [[LZPublishedCouponTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _publishedTable.tag = kCoupon_hasBeenPublished;
        _publishedTable.publishTableDelegate = self;
    }
    
    return _publishedTable;
}

- (LZFinishedCouponTableView *)finishedTable
{
    if (!_finishedTable) {
        _finishedTable = [[LZFinishedCouponTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _finishedTable.tag = kCoupon_hasBeenFinished;
        _finishedTable.finishTableDelegate = self;
    }
    
    return _finishedTable;
}
@end
